<template>
  <div class="login-content">
    <div class="login-content-body">
      <div class="login-content-body-title">
        <div class="header-title-text">
          <p class="title">山西省军区气象服务监测预警平台</p>
        </div>
      </div>
      <el-form
        ref="formRef"
        :model="formData"
        label-width="0px"
        size="large"
        :rules="rules"
        class="login-container"
      >
        <el-form-item label="" prop="username">
          <el-input
            v-model="formData.username"
            placeholder="请输入用户名"
            :disabled="loading"
            @keydown.enter="submitForm"
          >
            <template #prefix>
              <div class="form-prefix">
                <el-icon :size="22"><User /></el-icon>
              </div>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input
            v-model="formData.password"
            show-password
            placeholder="请输入密码"
            :disabled="loading"
            @keydown.enter="submitForm"
          >
            <template #prefix>
              <div class="form-prefix">
                <el-icon :size="22"><Lock /></el-icon>
              </div>
            </template>
          </el-input>
        </el-form-item>
        <div class="code">
          <el-form-item label="" prop="code">
            <el-input
              v-model="formData.code"
              placeholder="验证码"
              :disabled="loading"
              @keydown.enter="submitForm"
            >
              <template #prefix>
                <div class="form-prefix">
                  <el-icon :size="22"><Lock /></el-icon>
                </div>
              </template>
            </el-input>
          </el-form-item>
          <div class="codeImage" @click="refreshCaptcha">
            <img :src="CaptchaImage" alt="" />
          </div>
        </div>
        <el-form-item class="operation">
          <el-button
            v-loading="loading"
            class="login-btn"
            element-loading-background="rgba(12, 21, 54, 0.7)"
            @click="submitForm"
            ><p>登录</p></el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import { Lock, User } from '@element-plus/icons-vue'
import { useUserStore } from '@/store/modules/user'
import https from './api/api'
const store = useUserStore()
const router = useRouter()

const formRef = ref(null)
const CaptchaImage = ref()
const uuid = ref()
const formData = reactive({
  username: '',
  password: '',
  code: '',
  uuid: ''
})
const rules = reactive({
  username: [{ required: true, message: '请输入用户名', trigger: 'change' }],
  password: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'change'
    }
  ],
  code: [
    {
      required: true,
      message: '请输入验证码',
      trigger: 'change'
    }
  ]
})

const loading = ref(false)

const submitForm = () => {
  formRef.value.validate(async (valid, fields) => {
    if (valid) {
      loading.value = true
      formData.uuid = uuid.value
      https.login(formData).then(res => {
        loading.value = false
        if (res.code == 200) {
          store.token = res.token
          ElMessage.success('登录成功！')
          router.push({ path: '/home' })
        } else {
          formData.code = ''
          ElMessage.error(res.msg)
          getCaptchaImage()
        }
      })
    }
  })
}

const getCaptchaImage = () => {
  https.captchaImage().then(res => {
    if (res.code == 200) {
      CaptchaImage.value = 'data:image/jpeg;base64,' + res.img
      uuid.value = res.uuid
    }
  })
}
const refreshCaptcha = () => {
  getCaptchaImage()
}
onMounted(() => {
  if (store.isLogin) {
    router.push({ path: '/integrated-monitoring' })
  }
  getCaptchaImage()
})
</script>

<style lang="scss" scoped>
.login-content {
  width: 100%;
  height: 100%;
  padding-right: 160px;
  background: url('./images/bg.png') no-repeat;
  background-size: 100% 100%;
  display: flex;
  align-items: center;
  justify-content: center;

  .login-content-body {
    width: 758px;
    height: 754px;
    border-radius: 10px 10px 10px 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 0 55px;
    box-sizing: border-box;
    .login-container {
      background: url('./images/bg-login-form.png') no-repeat;
      background-size: 100% 100%;
      padding: 90px 70px 20px;
      margin-top: 30px;
    }
    .el-form-item {
      margin-top: 30px;
    }
    .el-form-item:not(.operation) {
      width: 407px;
      height: 57px;
      border-radius: 6px 6px 6px 6px;
      background: #165691;
      border: 1px solid #209cfc;
      padding: 0;
    }
    .el-form-item:nth-last-child(2) {
      width: 207px;
      height: 57px;
      border-radius: 6px 6px 6px 6px;
      background: #165691;
      border: 1px solid #209cfc;
      padding: 0;
    }
    .code {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .el-form-item {
        margin: 0 !important;
      }
    }
    .codeImage {
      width: 195px;
      height: 60px;
      img {
        width: 100%;
        height: 100%;
        border-radius: 6px;
      }
    }
    :deep(.el-input) {
      width: 450px;
      border: none;
      border-radius: 4px;

      .form-prefix {
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 16px;
        color: #fff;
      }

      .el-input__inner {
        color: #fff;
        font-size: 18px;
        &::placeholder {
          color: #fff;
        }
      }

      .el-input__wrapper {
        background-color: unset !important;
        box-shadow: none;
      }
    }

    .el-form-item.operation {
      width: 100%;

      :deep(.el-form-item__content) {
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .login-btn {
        width: 407px;
        height: 51px;
        border-radius: 6px;
        background: #219cfc;
        border: none;
        cursor: pointer;

        p {
          letter-spacing: 10px;
          font-size: 18px;
          font-weight: 400;
          color: #ffffff;
        }
      }
    }

    .login-content-body-title {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      font-size: 30px;
      font-weight: 400;
      color: #3db1fb;

      .logo {
        width: 96px;
        height: 96px;
        margin-bottom: 30px;
      }

      .header-title-text {
        flex: 1;
        height: 82px;
        display: flex;
        justify-content: center;
        flex-direction: column;

        .title {
          font-family: AlimamaShuHeiTi, AlimamaShuHeiTi;
          font-weight: bold;
          font-size: 48px;
          background: linear-gradient(to bottom, #fff, #fff, #1e92eb, #6a9abb);
          -webkit-background-clip: text;
          color: transparent;
          display: inline-block;
          white-space: nowrap;
        }

        .english {
          font-size: 12px;
          letter-spacing: 2px;
          color: #bebebe;
        }
      }
    }
  }
}
</style>
